/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Negocio;


import java.sql.Time;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.regex.Matcher;

import java.util.regex.Pattern;

/**
 *
 * @author v3550
 */
public class Validar {
    private static Validar  referencia = null;
    
   
    private Validar(){}
    
    public static Validar getInstace()
    {
         return referencia = (referencia == null) ? new Validar(): referencia;
    }
    
    public  boolean validaString(String entrada) {
        return entrada.contains(" ");
    }
    
    public boolean validaInteger(String entrada) {
        if (entrada == null || entrada.equals(" "))  
            return false;  
        
        try 
        {
           int q = Integer.parseInt(entrada);
           
           if(q > 0)
                return true;
           else
               return false;
        }catch(Exception e)
        {
           return false;
        }
    }
    public boolean validaDouble(String data)
    {
       boolean ret = false;
       double d = -1;
       try
       {
         d = Double.parseDouble(data);
         
         if(d >= 0)
            ret = true;
         else 
             ret = false;
         
       }catch(Exception e)
       {
           return false;
       }
       
       return ret;
    }
    
    public  java.sql.Date validaData(String data){   
        
        if (data == null || data.equals(""))  
            return null;  
           
           String[] q = data.split("/");
           
           if(q.length !=3) return null; // DD[0]/ MM[1]/ YYYY[2]
        
           String nData = q[2]+"-"+q[1]+"-"+q[0];
           
           SimpleDateFormat formatador = new SimpleDateFormat("yyyy-MM-dd");  
           java.sql.Date date;
           try  
            {  
               date = new java.sql.Date(formatador.parse(nData).getTime()); 
            }  
            catch(ParseException ex)  
            {   
                date = null;
            }  
        return date;  
    }  
    
    public java.sql.Time validaHora(String entrada){
       
        if (entrada == null || entrada.equals("")) return null; 
              
        
         DateFormat formato = new SimpleDateFormat("HH:mm:ss");
         Time hora= null;
        try {  
             hora = new java.sql.Time(formato.parse(entrada).getTime());  
        } catch (Exception e) {  
    
            throw new RuntimeException(e.getMessage());  
        }  
        return hora;
    }
    

     
    public boolean validaEmail(String email)
    {
      //Set the email pattern string  
      Pattern p = Pattern.compile(".+@.+\\.[a-z]+");  
  
      //Match the given string with the pattern  
      Matcher m = p.matcher(email);  
  
      //check whether match is found   
      boolean matchFound = m.matches();  
  
      if (matchFound)  
       return true;
      else  
       return false;
    }
    
}
